package cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemyw;

import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemyw.vo.DpItemYwPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemyw.vo.DpItemYwRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemyw.vo.DpItemYwSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemYwDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemYwService;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP义乌制单货号详情")
@RestController
@RequestMapping("/erp/dp-item-yw")
@Validated
public class DpItemYwController {

    @Resource
    private DpItemYwService dpItemYwService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP义乌制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:create')")
    public CommonResult<Long> createDpItemYw(@Valid @RequestBody DpItemYwSaveReqVO createReqVO) {
        return success(dpItemYwService.createDpItemYw(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP义乌制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:update')")
    public CommonResult<Boolean> updateDpItemYw(@Valid @RequestBody DpItemYwSaveReqVO updateReqVO) {
        dpItemYwService.updateDpItemYw(updateReqVO);
        return success(true);
    }

//    @PutMapping("/split")
//    @Operation(summary = "ERP义乌制单货号详情拆单")
//    @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:split')")
//    public CommonResult<Boolean> splitDpItemYw(@RequestParam("no") String no,
//                                               @Valid @RequestBody List<DpItemYwSaveReqVO> createReqVOList) {
//        dpItemYwService.splitDpItemYw(no, createReqVOList);
//        return success(true);
//    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP义乌制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:delete')")
    public CommonResult<Boolean> deleteDpItemYw(@RequestParam("id") Long id) {
        dpItemYwService.deleteDpItemYw(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP义乌制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:query')")
    public CommonResult<DpItemYwRespVO> getDpItemYw(@RequestParam("id") Long id) {
        DpItemYwDO dpItemYw = dpItemYwService.getDpItemYw(id);
        return success(BeanUtils.toBean(dpItemYw, DpItemYwRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP义乌制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:query')")
    public CommonResult<PageResult<DpItemYwRespVO>> getDpItemYwPage(@Valid DpItemYwPageReqVO pageReqVO) {
        PageResult<DpItemYwDO> pageResult = dpItemYwService.getDpItemYwPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemYwRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP义乌制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-yw:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemYwExcel(@Valid DpItemYwPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemYwDO> list = dpItemYwService.getDpItemYwPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP义乌制单货号详情.xls", "数据", DpItemYwRespVO.class,
                        BeanUtils.toBean(list, DpItemYwRespVO.class));
    }

}